package demo;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> queue;


    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<Integer>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue.offer(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if (queue.isEmpty()) {
            return -1;
        }
        int size = queue.size();
        while(size > 1) {
            queue.offer(queue.poll());
            size--;
        }
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        if (queue.isEmpty()) {
            return -1;
        }
        int size = queue.size();
        while(size > 1) {
            queue.offer(queue.poll());
            size--;
        }
        int ret = queue.poll();
        queue.offer(ret);
        return ret;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
       MyStack myStack = new MyStack();
       myStack.push(1);
       myStack.push(2);
       System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }


}

